<?php

namespace app\common\service\arvato;

use app\common\arvato\CommonPush;
use app\common\model\jsm\Members as MembersModel;
use app\common\model\oms\Companies as CompaniesModel;
use app\common\model\jsm\Mps as MpsModel;
use app\common\model\jsm\Membersx as MembersxModel;

/**
 * Description of arvato
 * 本类主要针对与arvato会员相关同步入队的功能
 */
class ArvatoBaseService {

    /**
     * 推送给arvato的入队方法【异步入队推】
     * @param type $data
     * @param type $funcName
     * @param type $redisKey
     * @param type $type
     */
    protected static function ArvatoPush($data,$funcName = '',$redisKey = '',$type ='rpush'){
        $CommonPush = new CommonPush();
        $CommonPush->pushToRedis($data, $funcName, $redisKey, $type);
    }

    /**
     * 获取粉丝和会员(微信)
     * @param type $openId
     * @param type $companyId
     * @return array
     */
    protected function getFans($openId = '',$companyId = ''){
        
        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsInfoByCondition(['o_id'=>$companyId]);

        $MembersModel = new MembersModel($mps['mp_id']);
        $Members = $MembersModel->getMembersByCondition(['openid'=>$openId],
                'headimgurl as wx_avatar_url,'
                . 'openid as wx_openid,'
                . 'nickname as wx_nick_name,'
                . 'gender as wx_sex,'
                . 'language as wx_language,'
                . 'city as wx_city,'
                . 'province as wx_province,'
                . 'country as wx_country,'
                . 'from_unixtime(subscribe_time) as wx_subscribe_time,'
                . 'unionid as wx_unionid');

        return $Members;
    }
    
    
    
    /**
     * 获取粉丝和会员（小程序）
     * @param type $wx_union_id
     * @param type $companyId
     * @return array
     */
    protected function getFansx($wx_union_id = '',$companyId = ''){
        
        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsInfoByCondition(['o_id'=>$companyId]);

        $MembersxModel = new MembersxModel($mps['mp_id']);
        $Membersx = $MembersxModel->getMembersxByCondition(['unionid'=>$wx_union_id],
                'headimgurl as wx_avatar_url,'
                . 'openid as wx_openid,'
                . 'nickname as wx_nick_name,'
                . 'gender as wx_sex,'
                . 'language as wx_language,'
                . 'city as wx_city,'
                . 'province as wx_province,'
                . 'country as wx_country,'
                . 'from_unixtime(subscribe_time) as wx_subscribe_time,'
                . 'unionid as wx_unionid');

        return $Membersx;
    }
    
    
    /**
     * 根据companyCode获取companyId
     * @param type $companyCode
     * @return type
     * @throws \Exception
     */
    protected function getCompanyIdByCompanyCode($companyCode) {
        //判断CompanyCode是否正确
        $companiesModel = new CompaniesModel();
        $companyInfo = $companiesModel->getCompanyInfoByCondition("company_id", ['company_code' => $companyCode], true);
        $company_id = $companyInfo['company_id'];
        if (!$company_id) {
            throw new \Exception("companyCode don't find company_id");
        }

        return $company_id;
    }
}
